package User;
import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import constant.Constant;
import util.AnalyzingBorrowedBook;

import java.util.Scanner;

public class NormalUser extends User {
    private PairOfUidAndBookId[] pairOfUidAndBookIds;
    private PairOfUidAndBookId pairOfUidAndBookId;
    private int borrowedCount;
    private static final int BORROW_BOOK_MAX_NUM = 5;
    private final AnalyzingBorrowedBook analyzingBorrowedBook=new AnalyzingBorrowedBook();
    Scanner scanner=new Scanner(System.in);
    private Library library;
    public NormalUser(String userName,int userID){
        super(userName,userID,"普通用户");
        library= Library.getLibrary();
        loadBorrowedBook();
    }
    private void loadBorrowedBook(){
        PairOfUidAndBookId[] allBorrowedBook;
        allBorrowedBook=analyzingBorrowedBook.loadObject(Constant.BORROWED_BOOK_FILE_NAME);
        pairOfUidAndBookIds=new PairOfUidAndBookId[BORROW_BOOK_MAX_NUM];
        if(allBorrowedBook==null){
            borrowedCount=0;
        }else{
            if(pairOfUidAndBookIds.length<allBorrowedBook.length){
                pairOfUidAndBookIds=new PairOfUidAndBookId[allBorrowedBook.length];
            }
            for(int i=0;i<allBorrowedBook.length;i++){
                pairOfUidAndBookIds[i]=allBorrowedBook[i];
            }
        }
        borrowedCount=allBorrowedBook.length;
    }
    private void storeBorrowedBook(){
        analyzingBorrowedBook.storeObject(pairOfUidAndBookIds,Constant.BORROWED_BOOK_FILE_NAME);
    }
    public void borrowBook(){
        System.out.println("目前书籍如下");
        library.displayBook();
        System.out.println("请输入你要借的书籍ID");
        int bookId= scanner.nextInt();
        scanner.nextLine();
        //1. 检查如果没有书籍，就不能借阅
        if(library.getCountBook()==0){
            System.out.println("书架上已经没有书了，不能借了");
            return;
        }
        //2，判断书架有没有你要借的书籍名字；
        loadBorrowedBook();
        Book book=library.searchById(bookId);
        if(book==null){
            System.out.println("没有你要借的书籍");
            return;
        }
        //3.检查 是否当前要借阅的书籍 之前被自己 或者 别人借阅过
        for(int i=0;i<borrowedCount;i++){
            PairOfUidAndBookId pairOfUidAndBookId=pairOfUidAndBookIds[i];
            if(pairOfUidAndBookId.getBookId()==book.getBookId()){
                if (getUserID() == pairOfUidAndBookId.getUserId()) {
                    System.out.println("该书已经被你借阅过了，你的ID是：" + getUserID());
                    return;
                } else {
                    System.out.println("该书已经被其他人借阅过了，他的ID是：" + pairOfUidAndBookId.getUserId());
                    return;
                }
            }
            }
        library.borrowBook(bookId);
        PairOfUidAndBookId pairOfUidAndBookId=new PairOfUidAndBookId(userID,book.getBookId());
        pairOfUidAndBookIds[borrowedCount]=pairOfUidAndBookId;
        borrowedCount++;
        storeBorrowedBook();
        System.out.println("借阅成功");
        }

    public int display() {
        System.out.println("普通⽤⼾ " + userName + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个⼈借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    public void returnBook(){
        System.out.println("目前书架有如下书籍：");
        library.displayBook();
        System.out.println("请输入你要归还的书籍ID");
        int bookId= scanner.nextInt();
        scanner.nextLine();
        if(library.getCountBook()==0){
            System.out.println("书架上没有书不需要归还");
            return;
        }
        loadBorrowedBook();
        Book book=library.searchById(bookId);
        if(book==null){
            System.out.println("没有你要归还的书籍，不能归还");
            return;
        }
        for(int i=0;i<borrowedCount;i++){
            if(pairOfUidAndBookIds[i].getBookId()==book.getBookId()){
                if(pairOfUidAndBookIds[i].getUserId()==getUserID()){
                    library.returnBook(bookId);
                    System.out.println("归还图书"+book.getTitle()+"成功");
                    pairOfUidAndBookIds[i]=pairOfUidAndBookIds[borrowedCount-1];
                    pairOfUidAndBookIds[borrowedCount-1]=null;
                    borrowedCount--;
                    storeBorrowedBook();
                }else{
                    System.out.println("该书籍不是你借的，不能归还"+book.getTitle());
                }
                return;
            }
            System.out.println("不能归还这本书"+book.getTitle());
        }
    }
    public void viewBorrowBooks(){
        loadBorrowedBook();
        System.out.println("您目前的借阅情况如下");
        if(borrowedCount==0){
            System.out.println("您没有借阅过图书");
        }else{
            boolean lag=false;
            for(int i=0;i<borrowedCount;i++){
                if(pairOfUidAndBookIds[i].getUserId()==userID){
                    lag=true;
                    Book book=library.searchById(pairOfUidAndBookIds[i].getBookId());
                    System.out.println(book);
                }
            }
            if(!lag){
                System.out.println("你没有借阅过图书");
            }
        }

    }

}
